# -*- coding: utf-8 -*-
import requests, json, time
import urllib.request


def getSogouImag(category, length, path):
    imgs = requests.get('http://pic.sogou.com/pics/channel/getAllRecomPicByTag.jsp?category=' + category + '&tag=全部&start=0&len=' + str(length))
    sgo = json.loads(imgs.text)
    sgo = sgo['all_items']
    imgs_url = []
    names = []
    for s in sgo:
        imgs_url.append(s['pic_url'])
        names.append(s["title"])
    m = 1
    for img_url, name in zip(imgs_url, names):
        print(str(m) + '--' + name + '--' + img_url)
        time.sleep(0.2)
        save_path = path + str(m) + '_' + name
        urllib.request.urlretrieve(img_url, save_path + '.jpg')
        m = m + 1
    print('\nDownload complete!')


if __name__ == '__main__':
    num = int(input("请输入您要下载的壁纸的数目："))
    getSogouImag('壁纸', num, 'H:\\新建文件夹\\搜狗壁纸\\')
